/*
 * @Author: Leo.wang wanglizhigs@163.com
 * @Date: 2024-09-02 22:47:16
 * @LastEditors: Leo.wang wanglizhigs@163.com
 * @LastEditTime: 2024-09-03 22:52:12
 * @FilePath: /metaverse-manage/app/controller/User.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Controller } from 'egg';
import { response, responsePage } from '../utils/common';
import { initUser, updateUser } from '../entity/User';
import { requestSessionToWx, getStableToken, getUserPhone } from '../utils/wchart';


export default class UserController extends Controller {
  // 查询所有
  public async queryAllUsers() {
    const { ctx } = this;
    const result = await ctx.service.user.queryAllUsers();
    ctx.body = response(result);
  }
  public async queryUserById() {
    const { ctx } = this;
    const { id } = ctx.params;
    const result = await ctx.service.user.queryUserById(Number(id));
    ctx.body = response(result);
  }
  public async queryUserByOpenid() {
    const { ctx } = this;
    const { openid } = ctx.params;
    const result = await ctx.service.user.queryUserByOpenid(openid);
    ctx.body = response(result);
  }

  public async queryUserByPhone() {
    const { ctx } = this;
    const { phone } = ctx.params;
    const result = await ctx.service.user.queryUserByPhone(phone);
    ctx.body = response(result);
  }


  public async createUser() {
    const { ctx } = this;
    const user = initUser({ ...ctx.request.body });
    const result = await ctx.service.user.createUser(user);
    ctx.body = response(result);
  }
  public async updateUser() {
    const { ctx } = this;
    const { id } = ctx.params;
    const user = updateUser({ ...ctx.request.body });
    const result = await ctx.service.user.updateUser(Number(id), user);
    ctx.body = response(result);
  }
  public async removeUser() {
    const { ctx } = this;
    const { id } = ctx.params;
    const result = await ctx.service.user.removeUser(Number(id));
    ctx.body = response(result);
  }

  public async queryAllUsersByPage() {
    const { ctx } = this;
    const { pageSize, page, ...params } = ctx.query || { pageSize: 10, page: 1 };

    const { count } = await ctx.service.user.countAllUsers();
    const result = await ctx.service.user.queryAllUsersByPage({
      pageSize: Number(pageSize),
      page: Number(page),
      ...params,
    });
    console.log('count', count);
    ctx.body = responsePage(result, {
      pageSize: Number(pageSize), page: Number(page), total: count || 0,
    });
  }

  public async login() {
    const { ctx } = this;
    const { code, detail, userInfo } = ctx.request.body;
    console.log('login', code, detail, userInfo);
    const openid = await requestSessionToWx(code);
    console.log('openid', openid);
    const user = await ctx.service.user.queryUserByOpenid(openid);
    if (user) {
      console.log('user', user);
      ctx.body = response(user);
    } else {
      const accessToken = await getStableToken();
      console.log('access_token', accessToken);
      const phoneInfo = await getUserPhone(detail.code, accessToken);
      console.log('phoneInfo', phoneInfo);

      const result = await ctx.service.user.createUser(initUser({
        openid,
        nick_name: userInfo.nickName,
        phone: phoneInfo?.purePhoneNumber,
        gender: userInfo.gender,
        avatar: userInfo.avatarUrl,
      }));
      console.log('result', result);
      const newUser = await ctx.service.user.queryUserByOpenid(openid);
      ctx.body = response(newUser);
    }

  }

}

